package admin.forms;

import helpers.StringHelper;

import java.net.URL;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;

import debugs.LinkDebug;

import entities.Link;

public class LinkForm {
	public static TreeMap<String, Object> checkValid(HttpServletRequest request, Link link) {
		TreeMap<String, Object> form = new TreeMap<String, Object>();
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		String id = (String) request.getParameter("id");
		String name = (String) request.getParameter("name");
		String description = (String) request.getParameter("description");
		String url = (String) request.getParameter("url");
		String type = (String) request.getParameter("type");
		String category = (String) request.getParameter("category");
		String urlReciprocal = (String) request.getParameter("urlReciprocal");
		String sourceReciprocal = (String) request.getParameter("sourceReciprocal");
		String permission = (String) request.getAttribute("permission");
		String status = (String) request.getParameter("status");
		
		if (id != null) {
			link.setId(id);
		}
		
		if (name == null || name.equals("")) {
			check.put("name", "Name is not empty");
			link.setName(name);
		} else {
			link.setName(name);
		}
		
		if (name != null && !name.isEmpty()) {
			String alias = StringHelper.replace(name);
			if (!alias.isEmpty()) {
				link.setAlias(alias);
			}
		}
		
		if (description == null || description.equals("")) {
			check.put("description", "Description is not empty");
			link.setDescription("");
		} else {
			link.setDescription(description);
		}
		
		if (url == null || url.equals("")) {
			check.put("url", "Url is not empty!");
			link.setUrl(url);
		} else {
			try {
				link.setUrl(url);
				URL sUrl = new URL(link.getUrl());
			} catch (Exception e) {
				check.put("url", "Url not valid!");
			}
		}
		
		if (type != null && type.equals("")) {
			check.put("type", "type is not empty");
		} else if (type != null) {
			link.setType(type);
		}
		
		if (category == null || category.equals("")) {
			check.put("category", "Category is not empty");
		} else {
			if (link.getCategory().equals(category)) {
				link.setCategory(category);
			} else {
				link.setLastCategory(link.getCategory());
				link.setCategory(category);
			}
		}
		
		if (link.getType() != null && link.getType().equalsIgnoreCase("reciprocal") && (permission == null || !permission.equalsIgnoreCase("admin"))) {
			if(urlReciprocal.equals(""))
			{
				check.put("urlReciprocal", "urlReciprocal is not empty");
			} else {
				link.setUrlReciprocal(urlReciprocal);
				if (!checkLinkReciprocal(link.getUrlReciprocal(), link.getSourceReciprocal())) {
					check.put("urlReciprocal", "Not contain HTML Reciprocal source in HTML Source link");
				}
			}
		}
		
		if (status != null && !status.equalsIgnoreCase("")) {
			link.setStatus(Integer.parseInt(status));
		}
		
		form.put("checkValid", check);
		form.put("link", link);
		
		return form;
	}
	
	private static boolean checkLinkReciprocal(String urlReciprocal, String sourceReciprocal) {
		boolean isContain = false;
		try {
			String content = Jsoup.connect(urlReciprocal).get().html();
			if (content.contains(sourceReciprocal)) {
				isContain = true;
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return isContain;
	}
}
